###############################################
#FIGURES F1 - F6
###############################################

library(ggplot2)
library(gridExtra)

#load cleaned data
setwd("C:/Users/jkendall/OneDrive/Documents/Empire Research/Competitive Guiltlessness/Journal Submission/Political Behaviour/Publication/Replication Files")
data_clean <- read.csv("./data/data_clean.csv")

#subset by treatment type (pure treatment type, no comparisons)
datac  <- subset(data_clean, trt == "control")
databr <- subset(data_clean, trt == "brit")
databe <- subset(data_clean, trt == "belg")

# function to create histograms for each outcome 
#0-10 outcomes
create_combined_hist_0_10 <- function(var_name, title_suffix) {
  
  # ---- compute common ymax across groups ----
  h1 <- hist(datac[[var_name]], breaks = seq(-0.5, 10.5, by = 1), plot = FALSE)
  h2 <- hist(databr[[var_name]], breaks = seq(-0.5, 10.5, by = 1), plot = FALSE)
  h3 <- hist(databe[[var_name]], breaks = seq(-0.5, 10.5, by = 1), plot = FALSE)
  
  ymax <- max(c(h1$counts, h2$counts, h3$counts))
  
  # ---- plots ----
  p1 <- ggplot(datac, aes_string(x = var_name)) +
    geom_histogram(binwidth = 1, boundary = -0.5,
                   fill = "lightblue", alpha = 0.7, color = "black") +
    scale_x_continuous(breaks = 0:10, limits = c(-0.5, 10.5)) +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Control Group", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  p2 <- ggplot(databr, aes_string(x = var_name)) +
    geom_histogram(binwidth = 1, boundary = -0.5,
                   fill = "lightgreen", alpha = 0.7, color = "black") +
    scale_x_continuous(breaks = 0:10, limits = c(-0.5, 10.5)) +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Identity Threat Only", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  p3 <- ggplot(databe, aes_string(x = var_name)) +
    geom_histogram(binwidth = 1, boundary = -0.5,
                   fill = "lightcoral", alpha = 0.7, color = "black") +
    scale_x_continuous(breaks = 0:10, limits = c(-0.5, 10.5)) +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Identity Threat + Whataboutism", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  grid.arrange(p1, p2, p3, ncol = 3)
}

# 0-5 outcomes
create_combined_hist_listitems <- function(title_suffix) {
  
  breaks <- seq(-0.25, 5.25, by = 0.5)
  
  # ---- compute common ymax ----
  h1 <- hist(datac$list_items, breaks = breaks, plot = FALSE)
  h2 <- hist(databr$list_items, breaks = breaks, plot = FALSE)
  h3 <- hist(databe$list_items, breaks = breaks, plot = FALSE)
  
  ymax <- max(c(h1$counts, h2$counts, h3$counts))
  
  # ---- plots ----
  p1 <- ggplot(datac, aes(x = list_items)) +
    geom_histogram(breaks = breaks,
                   fill = "lightblue", alpha = 0.7, color = "black") +
    scale_x_continuous(breaks = 0:5, limits = c(-0.5, 5.5)) +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Control Group", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  p2 <- ggplot(databr, aes(x = list_items)) +
    geom_histogram(breaks = breaks,
                   fill = "lightgreen", alpha = 0.7, color = "black") +
    scale_x_continuous(breaks = 0:5, limits = c(-0.5, 5.5)) +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Identity Threat Only", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  p3 <- ggplot(databe, aes(x = list_items)) +
    geom_histogram(breaks = breaks,
                   fill = "lightcoral", alpha = 0.7, color = "black") +
    scale_x_continuous(breaks = 0:5, limits = c(-0.5, 5.5)) +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Identity Threat + Whataboutism", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  grid.arrange(p1, p2, p3, ncol = 3)
}

#other distributions
create_combined_hist_spread <- function(var_name, title_suffix) {
  
  # ---- compute common ymax ----
  h1 <- hist(datac[[var_name]], breaks = 30, plot = FALSE)
  h2 <- hist(databr[[var_name]], breaks = 30, plot = FALSE)
  h3 <- hist(databe[[var_name]], breaks = 30, plot = FALSE)
  
  ymax <- max(c(h1$counts, h2$counts, h3$counts))
  
  # ---- plots ----
  p1 <- ggplot(datac, aes_string(x = var_name)) +
    geom_histogram(bins = 30,
                   fill = "lightblue", alpha = 0.7, color = "black") +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Control Group", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  p2 <- ggplot(databr, aes_string(x = var_name)) +
    geom_histogram(bins = 30,
                   fill = "lightgreen", alpha = 0.7, color = "black") +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Identity Threat Only", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  p3 <- ggplot(databe, aes_string(x = var_name)) +
    geom_histogram(bins = 30,
                   fill = "lightcoral", alpha = 0.7, color = "black") +
    scale_y_continuous(limits = c(0, ymax)) +
    labs(title = "Identity Threat + Whataboutism", x = title_suffix, y = "Frequency") +
    theme_minimal()
  
  grid.arrange(p1, p2, p3, ncol = 3)
}

#run function on outcomes
figF1 <- create_combined_hist_0_10("prej_direct_1", "Explicit Prejudice")
figF2 <- create_combined_hist_listitems("List Items")
figF3 <- create_combined_hist_spread("donation_amount", "BLM Donation Amount")
figF4 <- create_combined_hist_spread("natid_pca", "National Identification PCA")
figF5 <- create_combined_hist_0_10("empire_1", "Perception of Empire")
figF6 <- create_combined_hist_0_10("comparison_1", "Agreement with Whataboutism")

setwd("C:/Users/jkendall/OneDrive/Documents/Empire Research/Competitive Guiltlessness/Latex/R&R Figures")
ggsave("figF1.png", figF1, width = 15, height = 5, dpi = 300)
ggsave("figF2.png", figF2, width = 15, height = 5, dpi = 300)
ggsave("figF3.png", figF3, width = 15, height = 5, dpi = 300)
ggsave("figF4.png", figF4, width = 15, height = 5, dpi = 300)
ggsave("figF5.png", figF5, width = 15, height = 5, dpi = 300)
ggsave("figF6.png", figF6, width = 15, height = 5, dpi = 300)
